package cn.tenquare.webGateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebZuulFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 10;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("web前端网关，经过过了zuul过滤器");
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        String authorization = request.getHeader("Authorization");
        if (authorization != null && !"".equals(authorization)){
            RequestContext.getCurrentContext().addZuulRequestHeader("Authorization", authorization);
        }
        return null;
    }
}
